﻿Imports Sistema_InformacionNaviera.BC
Imports Sistema_InformacionNaviera.BE
Imports Sistema_InformacionNavier.UTIL


Public Class GestionarDesglose

    'Inherits System.Web.UI.Page
    Inherits PaginaBase

    Dim mOk As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim iKvje As Integer
            Dim iKbli As Integer

            CrearTablas(0)
            CrearTablas(1)

            iKvje = Session("Desglose_iKvje")
            iKbli = Session("Desglose_iKbli")

            CargarAgenteAduana("AA")

            BuscarDatosBlImpo(iKvje, iKbli)

            'If My.Request.Params("__EVENTTARGET") = "MiFuncionViaje" Then
            '    If Session("BPCodTarifa") = Nothing Then
            '        txtTarifaFlete.Text = String.Empty
            '    Else
            '        txtTarifaFlete.Text = Session("BPCodTarifa")
            '        txtDesTarifaFlete.Text = Session("BPDesTarifa")

            '    End If
            'End If



        End If
    End Sub


    Public Sub CargarAgenteAduana(ByVal sTipo As String)


        Dim listGeneralBE As List(Of GeneralBE) = New List(Of GeneralBE)
        Dim objGeneralBC As GeneralBC = New GeneralBC()

        Dim objUnidadSeleccione As GeneralBE = New GeneralBE
        objUnidadSeleccione.Codigo = 0
        objUnidadSeleccione.Descripcion = "[Seleccione]"
        listGeneralBE.Add(objUnidadSeleccione)
        objUnidadSeleccione.Codigo = sTipo

        listGeneralBE.AddRange(objGeneralBC.ListadoGeneral(objUnidadSeleccione))

        cboAgenteAduana.DataSource = listGeneralBE
        cboAgenteAduana.DataValueField = "Codigo"
        cboAgenteAduana.DataTextField = "Descripcion"
        cboAgenteAduana.DataBind()

    End Sub

    Public Sub BuscarDatosBlImpo(iViaje As Integer, iKbli As Integer)

        Try

            Dim objBlImpoBE As New BillOfLadingImpoBE
            Dim ImpoDesgloseBE As BillOfLadingImpoBE
            Dim objUtil As New Funciones
            Dim objBlImpoBC As New BillOfLadingImpoBC
            objBlImpoBE.KVJE = iViaje
            objBlImpoBE.KBLM = iKbli

            ImpoDesgloseBE = objBlImpoBC.ListarBlImpoDesglose(objBlImpoBE)
            If Not ImpoDesgloseBE Is Nothing Then
                lblNaveViaje.Text = ImpoDesgloseBE.desKVJE
                lblConsignatario.Text = ImpoDesgloseBE.desc_cod_cons
                lblBLMaster.Text = ImpoDesgloseBE.Nume
                lblTotalPeso.Text = ImpoDesgloseBE.Nume
                lblTotalBLHijos.Text = ImpoDesgloseBE.Nhijos
                lblRecepBL.Text = ImpoDesgloseBE.FecRevision
                lblEstado.Text = ImpoDesgloseBE.EstadoDes
            Else
                MsgBox("No se pudo cargar la información.", vbInformation, "Error")
            End If

        Catch ex As Exception
            ex.Message.ToString()
        End Try

    End Sub


    Protected Sub btnAgregar_Click(sender As Object, e As EventArgs) Handles btnAgregar.Click

        If Val(lblViaje.Text.Trim) <= 0 Then
            MsgBox("No se pudo cargar la información.", vbInformation, Me.Title)
            Exit Sub
        End If

        If Val(lblKBli.Text.Trim) <= 0 Then
            MsgBox("No se pudo cargar la información.", vbInformation, Me.Title)
            Exit Sub
        End If

        If txtNHijo.Text.Trim <> "" Then
            MsgBox("Debe ingresar el número de BL.", vbInformation, Me.Title)
            Exit Sub
        End If
        If cboAgenteAduana.SelectedIndex = 0 Then
            MsgBox("Debe seleccionar un agente de aduana.", vbInformation, Me.Title)
            Exit Sub
        End If

        If CDbl(txtPeso.Text.Trim) = 0 Then
            MsgBox("Debe ingresar un peso válido.", vbInformation, Me.Title)
            Exit Sub
        End If

        Dim drItem As DataRow
        Dim dt As DataTable
        dt = New DataTable
        dt = Session("tbTempsDesglose")

        If Not ValidaRepetido(dt, 3, txtNHijo.Text.Trim, "") Then
            'Dim objDesgloseBC As New DesgloseBC
            'Dim objDesgloseBE As New DesgloseBE
            'Dim objDesgloseDetBE As New DesgloseBE
            'objDesgloseBE.Kvje = lblViaje.Text.Trim
            'objDesgloseBE.KBle = lblKBli.Text.Trim
            'objDesgloseBE.Nume = txtNHijo.Text.Trim
            'objDesgloseBE.Csne = txtCodConsig.Text.Trim
            'objDesgloseBE.AgAduana = cboAgenteAduana.SelectedValue.Trim
            'objDesgloseBE.Peso = txtPeso.Text
            'objDesgloseBE.Obse = txtObs.Text.Trim
            'objDesgloseDetBE.Usu = "mfigueroa"

            '-------------------------------------------------------------------------------------
            'agrega datos a tabla temporal 
            '------------------------------------------------------------------------
            If dt.Rows.Count > 0 Then
                 
                     Dim dr As DataRow = dt.NewRow
                dr("KVJE") = Val(lblViaje.Text)
                dr("KBLE") = Val(lblKBli.Text)
                dr("KBLD") = 0
                dr("NUME") = txtNHijo.Text.Trim
                dr("CODEMBA") = txtCodConsig.Text.Trim
                dr("EMBA") = txtDesConsig.Text.Trim
                dr("CODAGAD") = cboAgenteAduana.SelectedValue.Trim
                dr("AGAD") = cboAgenteAduana.SelectedItem.Text.Trim
                dr("PESO") = txtPeso.Text
                dr("OBSE") = txtObs.Text.Trim
                  dt.Rows.Add(dr)
                Session("tbTempsDesglose") = dt
                Me.dgDesglose.DataSource = SelectDataTable(dt, "", "")
                Me.dgDesglose.DataBind()
                    Exit Sub
            Else
                Dim dr As DataRow = dt.NewRow
                dr("KVJE") = Val(lblViaje.Text)
                dr("KBLE") = Val(lblKBli.Text)
                dr("KBLD") = 0
                dr("NUME") = txtNHijo.Text.Trim
                dr("CODEMBA") = txtCodConsig.Text.Trim
                dr("EMBA") = txtDesConsig.Text.Trim
                dr("CODAGAD") = cboAgenteAduana.SelectedValue.Trim
                dr("AGAD") = cboAgenteAduana.SelectedItem.Text.Trim
                dr("PESO") = txtPeso.Text
                dr("OBSE") = txtObs.Text.Trim

                dt.Rows.Add(dr)
                Session("tbTempsDesglose") = dt
                Me.dgDesglose.DataSource = SelectDataTable(dt, "", "")
                Me.dgDesglose.DataBind()
            End If
        End If 
    End Sub

    Function SelectDataTable(ByVal dt As DataTable, ByVal filter As String, ByVal sort As String) As DataTable

        Dim rows As DataRow()
        Dim dtNew As DataTable
        dtNew = dt.Clone()
        rows = dt.Select(filter, sort)
        For Each dr As DataRow In rows
            dtNew.ImportRow(dr)
        Next
        Return dtNew

    End Function

    Public Shared Function ValidaRepetido(ByVal dt As DataTable, ByVal col As Integer, sTexto As String, ByVal sMsg As String) As Boolean
        Dim i As Integer
        Dim bRepetido As Boolean = False
        If Not dt Is Nothing Then
            If dt.Rows.Count > 0 Then
                For i = 0 To dt.Rows.Count - 1
                    If dt.Rows(i).Item(col).ToString.Trim = sTexto.Trim Then
                        'Alerta("El item seleccionado ya está registrado", False, String.Empty)
                        bRepetido = True
                        Exit For
                    End If
                Next
            End If
        End If
        Return bRepetido
    End Function

    Public Sub CrearTablas(iTipo As Integer)
        If iTipo = 0 Then
            Dim dt As New DataTable 
            dt.Columns.Add("KVJE")
            dt.Columns.Add("KBLE")
            dt.Columns.Add("KBLD")
            dt.Columns.Add("NUME")
            dt.Columns.Add("CODEMBA")
            dt.Columns.Add("EMBA")
            dt.Columns.Add("CODAGAD")
            dt.Columns.Add("AGAD")
            dt.Columns.Add("PESO")
            dt.Columns.Add("OBSE")
            Session("tbTempsDesglose") = dt
        ElseIf iTipo = 1 Then
            Dim dt As New DataTable 
            dt.Columns.Add("KVJE")
            dt.Columns.Add("KBLE")
            dt.Columns.Add("KBLD")
            dt.Columns.Add("NUME")
            dt.Columns.Add("PREF")
            dt.Columns.Add("NCTR")
            dt.Columns.Add("NUMCTR")
            dt.Columns.Add("COND")
            Session("tbTempDesgloseCTR") = dt
        End If

    End Sub

    Protected Sub btnAgregar0_Click(sender As Object, e As EventArgs) Handles btnAgregar0.Click
        If Val(lblViaje.Text.Trim) <= 0 Then
            MsgBox("No se pudo cargar la información.", vbInformation, Me.Title)
            Exit Sub
        End If

        If Val(lblKBli.Text.Trim) <= 0 Then
            MsgBox("No se pudo cargar la información.", vbInformation, Me.Title)
            Exit Sub
        End If

        If cboConCondicion.SelectedIndex = 0 Then
            MsgBox("Debe seleccionar un agente de aduana.", vbInformation, Me.Title)
            Exit Sub
        End If

       

        Dim drItem As DataRow
        Dim dt As DataTable
        dt = New DataTable
        dt = Session("tbTempDesgloseCTR")

        If Not ValidaRepetido(dt, 3, txtNHijo.Text.Trim, "") Then
         
            dt.Columns.Add("KVJE")
            dt.Columns.Add("KBLE")
            dt.Columns.Add("KBLD")
            dt.Columns.Add("NUME")
            dt.Columns.Add("NUMCTR")
            dt.Columns.Add("COND")
            '-------------------------------------------------------------------------------------
            'agrega datos a tabla temporal 
            '------------------------------------------------------------------------
            If dt.Rows.Count > 0 Then

                Dim dr As DataRow = dt.NewRow
                dr("KVJE") = Val(lblViaje.Text)
                dr("KBLE") = Val(lblKBli.Text)
                dr("KBLD") = 0
                dr("NUME") = txtNHijo.Text.Trim
                dr("PREF") = txtPref.Text.Trim
                dr("NCTR") = txtNume.Text.Trim
                dr("NUMCTR") = txtPref.Text.Trim + txtNume.Text.Trim
                dr("COND") = cboConCondicion.SelectedValue.Trim
                dt.Rows.Add(dr)
                Session("tbTempDesgloseCTR") = dt
                Me.dgDesgloseDeta.DataSource = SelectDataTable(dt, "", "")
                Me.dgDesgloseDeta.DataBind()
                Exit Sub
            Else
                Dim dr As DataRow = dt.NewRow
                dr("KVJE") = Val(lblViaje.Text)
                dr("KBLE") = Val(lblKBli.Text)
                dr("KBLD") = 0
                dr("NUME") = txtNHijo.Text.Trim
                dr("PREF") = txtPref.Text.Trim
                dr("NCTR") = txtNume.Text.Trim
                dr("NUMCTR") = txtPref.Text.Trim + txtNume.Text.Trim
                dr("COND") = cboConCondicion.SelectedValue.Trim
                dt.Rows.Add(dr)
                Session("tbTempDesgloseCTR") = dt
                Me.dgDesgloseDeta.DataSource = SelectDataTable(dt, "", "")
                Me.dgDesgloseDeta.DataBind()
            End If
        End If
    End Sub

    Private Sub dgDesglose_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles dgDesglose.RowCommand
        If dgDesglose.Rows.Count > 0 Then
            Dim sCodigo As String = Convert.ToString(e.CommandArgument)
            Dim sMensaje As String = String.Empty

            Select Case e.CommandName  '.ToLower
                Case "Eliminar"

                    Dim i As Integer = 0
                    Dim rwEliminar As String
                    Dim dt As New DataTable
                    dt = Session("tbTempDesglose")

                    If dt.Rows.Count > 0 Then

                        Do While i < dt.Rows.Count
                            rwEliminar = dt.Rows(i).Item("NUME").ToString

                            If rwEliminar = sCodigo Then
                                dgDesglose.Rows(i).Visible = False
                                dt.Rows.Remove(dt.Rows(i))
                            End If

                            i = i + 1
                        Loop

                    End If

                    dgDesglose.DataSource = dt
                    dgDesglose.DataBind()

            End Select
        End If
    End Sub 

    Private Sub dgDesgloseDeta_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles dgDesgloseDeta.RowCommand
        If dgDesgloseDeta.Rows.Count > 0 Then
            Dim sCodigo As String = Convert.ToString(e.CommandArgument)
            Dim sMensaje As String = String.Empty

            Select Case e.CommandName  '.ToLower
                Case "Eliminar"

                    Dim i As Integer = 0
                    Dim rwEliminar As String
                    Dim dt As New DataTable
                    dt = Session("tbTempDesgloseCTR")

                    If dt.Rows.Count > 0 Then

                        Do While i < dt.Rows.Count
                            rwEliminar = dt.Rows(i).Item("NUMCTR").ToString

                            If rwEliminar = sCodigo Then
                                dgDesgloseDeta.Rows(i).Visible = False
                                dt.Rows.Remove(dt.Rows(i))
                            End If

                            i = i + 1
                        Loop

                    End If

                    dgDesgloseDeta.DataSource = dt
                    dgDesgloseDeta.DataBind()

            End Select
        End If
    End Sub

    Protected Sub btnNuevo_Click(sender As Object, e As EventArgs) Handles btnNuevo.Click
        'Dim strRepetido As String = ""
        'Dim bError As Boolean = False
        'mOk = True 
        ''obtenemos el detalle para validarlo

        'Dim dtDesglose As New DataTable
        'Dim dtDesgloseCtr As New DataTable 

        'dtDesglose = Session("tbTempDesglose ")
        'dtDesgloseCtr = Session("tbTempDesgloseCTR") 

        ' ''''''  

        'If Session("TipoEvento") = 0 Then


        '    bError = GrabaDetalle(0, dtCntr)
        '    bError = GrabaDetalle(1, dtBl) 

        '    ElseIf kAcu = -1 Then
        '        MsgBox("Ya existe un acuerdo con el mismo número", vbInformation, Me.Title)
        '        mOk = False
        '        Exit Sub
        '    Else
        '        MsgBox("Hubo problemas con el ingreso de Días Libres. El proceso no puede continuar.", vbInformation, Me.Title)

        '        mOk = False
        '        Exit Sub
        '    End If
        'Else

        '    objDLibBe.Kdli = Session("CodigoDL")
        '    kAcu = Session("CodigoDL")
        '    bExito = objDLibBC.ModificarDiasLibres(objDLibBe)
        '    If bExito Then
        '        objDLibBe.Tipo = "D"
        '        objDLibBC.EliminarDiasLibres(objDLibBe, bExito, "")

        '        bError = GrabaDetalle(0, kAcu, dtCntr)
        '        bError = GrabaDetalle(1, kAcu, dtBl)
        '        bError = GrabaDetalle(2, kAcu, dtTipies)
        '        bError = GrabaDetalle(3, kAcu, dtPto)
        '        bError = GrabaDetalle(4, kAcu, dtPais)
        '        bError = GrabaDetalle(5, kAcu, dtArea)

        '    Else
        '        MsgBox("Hubo problemas con el ingreso de Días Libres. El proceso no puede continuar.", vbInformation, Me.Title)

        '        mOk = False
        '        Exit Sub
        '    End If
        'End If

        'Response.Redirect("Consultar_DiasLibres.aspx", True)
    End Sub

    'Function GrabaDetalle(iTipo As Integer, dt As DataTable) As Boolean
    '    Dim objDesgloseBC As New DesgloseBC
    '    Dim objDesgloseBE As DesgloseBE
    '    Dim bExito As Boolean = False
    '    Dim i As Integer

    '    For i = 0 To dt.Rows.Count - 1
    '        objDesgloseBE = New DesgloseBE

    '        If iTipo = 0 Then
    '            objDesgloseBE.Kvje = Val(dt.Rows(i).Item(0).ToString)
    '            objDesgloseBE.KBle = Val(dt.Rows(i).Item(1).ToString)
    '            objDesgloseBE.Nume = Val(dt.Rows(i).Item(3).ToString)
    '            objDesgloseBE.Csne = Val(dt.Rows(i).Item(4).ToString)
    '            objDesgloseBE.AgAduana = Val(dt.Rows(i).Item(6).ToString)
    '            objDesgloseBE.Peso = Val(dt.Rows(i).Item(8).ToString)
    '            objDesgloseBE.Obse = Val(dt.Rows(i).Item(9).ToString)
    '            objDesgloseBE.Usu = "mfigueroa"
    '            'ElseIf iTipo = 1 Then

    '            '    dr("KVJE") = Val(lblViaje.Text)
    '            '    dr("KBLE") = Val(lblKBli.Text)
    '            '    dr("KBLD") = 0
    '            '    dr("NUME") = txtNHijo.Text.Trim
    '            '    dr("PREF") = txtPref.Text.Trim
    '            '    dr("NCTR") = txtNume.Text.Trim
    '            '    dr("NUMCTR") = txtPref.Text.Trim + txtNume.Text.Trim
    '            '    dr("COND") = cboConCondicion.SelectedValue.Trim

    '            '    objDesgloseBE.Viaje = Val(dt.Rows(i).Item(1).ToString)
    '            '    objDesgloseBE.Kbli = Val(dt.Rows(i).Item(2).ToString)
    '        End If
    '        objDesgloseBE.Usu = "Mfigueroa"
    '        bExito = objDesgloseBC.InsertarDiasLibresDet(objDesgloseBE, iTipo)
    '    Next
    'End Function
End Class